Ext.define('AmpleHarvest.objects.querymanager.cQueryManager', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.ampleHarvest-objects-querymanager-cquerymanager',
    m_str_view_type	: 'full',
    str_sourceName	: false,
    requires		:
    [
     	'AmpleHarvest.DATA.Store.cManager'
     ],  
    title: 'Query Manager',
    viewConfig: {
		forceFit : true,
		disableSelection : true
	},

	stripeRows: true,

    initComponent: function()
    {
    	var obj_This = this;

    	this.store = Ext.create('AmpleHarvest.DATA.Store.cManager');
    	
    	if(this.str_sourceName)
    	{
    		this.store.set_ExtraParams({
    			source_name	: this.str_sourceName
    		});
    	}
    	
    	this.add_columns(this.check_admin_features());
    	
    	var obj_Date = new Date;
    	this.id = obj_Date.getTime() + "_GridView";

    	this.m_str_DeleteID = '';

        this.store.load();
        
    	this.callParent();
    },
    
    check_admin_features : function()
    {
    	var arr_actions = new Array();
    	var obj_this = this;
    	if(this.m_bool_saved_query_add)
    	{
    		arr_actions.push(
    				{
	        		  iconCls	: 'query-delete',
	        		  tooltip	: 'Delete Query',
	        		  handler	: function(grid, rowIndex, colIndex)
	        		  {
	        			  	var rec = obj_this.getStore().getAt(rowIndex);
		    	  			obj_this.m_str_DeleteID = rec.data.id;
		    	  			Ext.MessageBox.confirm(
		    						"Confirm Delete",
		    						"Are you sure you want to delete \""+rec.data.q_title+"\"?",
		    						this.deleteQuery,
		    						this);
			      		},
			      		scope	: this
        	  	  	});
    	}
    	if(this.m_bool_saved_query_edit)
    	{
			arr_actions.push(
			{
				  iconCls	: 'query-edit',
				  tooltip	: 'Edit Query',
				  handler	: function(grid, rowIndex, colIndex)
				  {
					  	var rec = obj_this.getStore().getAt(rowIndex);
				  		m_obj_AmpleHarvest_ViewPort
				  				.fireEvent(
				  						AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_SAVED_QUERY_EDIT,
				  						rec
				  						);
				  }
			});
    	}
    	if(this.m_str_view_type != 'full')
    	{
    	arr_actions.push(
    			{
	        	  	  iconCls	: 'query-load',
		      	  	  tooltip	: 'Load Query',
		      	  	  scope		: this,
		      	  	  handler	: function(grid, rowIndex, colIndex)
		      	  	  {	  
		      	  		  var rec = obj_this.getStore().getAt(rowIndex);
		      	  		  var str_config_path = this.up('ampleharvest-views-search-main').obj_config.self;
//		      	  		  var str_config_path = this.up('ampleharvest-views-search-main').obj_form_info.str_config_path;
    				      var obj_config = eval(str_config_path);
    				      
		      	  		  obj_config.run = Ext.decode(rec.data.q_string);
		      	  		  
		      	  		  m_obj_AmpleHarvest_ViewPort
									.fireEvent(AmpleHarvest.eventHandler.cMenuEvents.C_STR_EVENT_GO_SEARCH,
											obj_config
											);
		      	  	  }
    	  	  
				}
    			);
    	}
    	if(Ext.isArray(arr_actions))
    	{
    		return arr_actions;    	
    	}
    	return new Array();
    },
    
    add_columns	: function(arr_actions)
    {
    	var arr_final = [
    	       	     {
    	       	      xtype			: 'actioncolumn',
    	           	  width			: 65,
    	           	  fixed			: true,
    	           	  menuDisabled	: true,
    	           	  items			: arr_actions
    	   	        },
    	   	        {
    	   	            header   : 'Title', 
    	   	            flex		: 1,
    	   	            sortable : true, 
    	   	            dataIndex: 'q_title'
    	   	        }];
    	       	
       	if(this.m_str_view_type == 'full')
       	{
       		var arr_full = [
       		{
   	            header   : 'Schedule Structure (H,D,DOTW,Y)',
   	            flex		: 1,
   	            sortable : false, 
   	            dataIndex: 'cron'
   	        },
   	        {
   	            header   : 'Last Ran', 
   	            flex		: 1,
   	            sortable : true, 
   	            dataIndex: 'last_run'
   	        },
   	        {
   	            header   : 'Next Run', 
   	            flex		: 1,
   	            sortable : true, 
   	            dataIndex: 'next_run'
   	        },
   	        {
   	            header   	: 'Source',
   	            flex		: 1,
   	            sortable 	: true, 
   	            dataIndex	: 'source_name'
   	        }
   	        ];
       		arr_final = arr_final.concat(arr_full);
       	}
       	this.columns = arr_final;
    	       	
    },
    
    refresh	: function()
    {
    	this.store.load();
    },
    
    get_CurSel	: function()
    {
		return this.getSelectionModel().getSelection();
    },
    
    deleteQuery: function(obj_Btn)
    {
    	if(obj_Btn == 'yes')
    	{    	
	      	var conn = new Ext.data.Connection();
	      	conn.request(
	      	{
	      	    url: obj_config.C_STR_EXTERNAL_PATH+'/savedquery/deleteQuery.php',
	      	    method: 'POST',
	      	    params: 
	      	    {
	      	    	"ID": this.m_str_DeleteID
	      	    },
	      	    success: function(responseObject) 
	      	    {
	    	      	lastOptions = this.store.lastOptions;
	    			this.store.load(lastOptions);  
	      	        Ext.MessageBox.alert(
	      				"Success",
	      				"Your query has been removed");
	      		},
	      	    failure: function() 
	      	    {
	      	        Ext.MessageBox.alert(
	      				"Failure",
	      				"Something went wrong!");
	      	    },
	      	    scope : this
	      	});
    	}
    }
});

